using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace BlockOS.Client.GUI
{
    [ExecuteAlways]
    public class RingGraphic : Graphic, ICanvasRaycastFilter
    {
        [SerializeField]
        private int iterCount = 10;
        [SerializeField]
        private float innerRadiusFactor = 1f;

        public Image m;

        public bool IsRaycastLocationValid(Vector2 sp, Camera eventCamera)
        {
            if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, sp, eventCamera, out var _))
            {
                return false;
            }
            return true;
        }

        protected override void OnPopulateMesh(VertexHelper vh)
        {
            vh.Clear();
            var rect = GetPixelAdjustedRect();
            Color32 color32 = color;
            var radius = rect.width / 2.0f;
            var innerRadius = radius * innerRadiusFactor;
            var center = new Vector3(rect.x + radius, rect.y + radius, 0);
            var uvCenter = new Vector3(0.5f, 0.5f, 0);
            var dir = Vector3.up;
            var angleStep = 360.0f / iterCount;
            
            
            if (Mathf.Approximately(innerRadius, 0))
            {
                for (int i = 0; i < iterCount; i++)
                {
                    var next = Quaternion.AngleAxis(angleStep, Vector3.back) * dir;
                    var lt = center + dir * radius;
                    var rt = center + next * radius;

                    var halfDir = 0.5f * dir;
                    var halfNext = 0.5f * next;
                    var uvLt = uvCenter + halfDir;
                    var uvRt = uvCenter + halfNext;

                    var count = vh.currentVertCount;

                    vh.AddVert(center, color32, uvCenter);
                    vh.AddVert(rt, color32, uvRt);
                    vh.AddVert(lt, color32, uvLt);

                    vh.AddTriangle(count + 0, count + 1, count + 2);

                    dir = next;
                }
            }
            else
            {
                for (int i = 0; i < iterCount; i++)
                {
                    var next = Quaternion.AngleAxis(angleStep, Vector3.back) * dir;
                    var lt = center + dir * radius;
                    var lb = center + dir * innerRadius;
                    var rt = center + next * radius;
                    var rb = center + next * innerRadius;

                    var halfDir = 0.5f * dir;
                    var halfNext = 0.5f * next;
                    var uvLt = uvCenter + halfDir;
                    var uvLb = uvCenter + halfDir * innerRadiusFactor;
                    var uvRt = uvCenter + halfNext;
                    var uvRb = uvCenter + halfNext * innerRadiusFactor;

                    var count = vh.currentVertCount;

                    vh.AddVert(lb, color32, uvLb);
                    vh.AddVert(rb, color32, uvRb);
                    vh.AddVert(rt, color32, uvRt);
                    vh.AddVert(lt, color32, uvLt);

                    vh.AddTriangle(count + 0, count + 1, count + 2);
                    vh.AddTriangle(count + 2, count + 3, count + 0);

                    dir = next;
                }
            }
        }
    }
}
